package com.abocode.aichat.configuration;

import com.abocode.aichat.server.context.channel.ProtocolChannelRegistryImpl;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.context.delivery.inbound.InboundServerDeliver;
import com.abocode.aichat.server.context.delivery.outbound.OutboundServerDeliver;
import com.abocode.aichat.server.context.pipeline.UserOnlinePipeline;
import com.abocode.aichat.server.context.pipeline.MessagePipeline;
import com.abocode.aichat.server.context.worker.ProtocolMessageStanzaWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class AppMessengerConfiguration {
    @Autowired
    private UserOnlinePipeline userOnlinePipeline;
    @Autowired
    private MessagePipeline messagePipeline;
    @Autowired
    private AiChatProperties aiChatProperties;
    @Bean
    public ProtocolChannelRegistryImpl protocolChannelRegistryImpl() {
        ProtocolChannelRegistryImpl protocolChannelRegistry = new ProtocolChannelRegistryImpl();
        protocolChannelRegistry.setUserOnlinePipeline(userOnlinePipeline);
        return protocolChannelRegistry;
    }

    @Bean
    public OutboundServerDeliver outboundServerDeliver() {
        return new OutboundServerDeliver();
    }

    @Bean
    public InboundServerDeliver inboundServerDeliver() {
        return new InboundServerDeliver();
    }

    @Bean
    public ProtocolMessageStanzaWorker protocolStanzaWorker() {
        return new ProtocolMessageStanzaWorker();
    }

    @Bean
    public ServerRuntimeContext serverRuntimeContext() {
        ServerRuntimeContext serverRuntimeContext = new ServerRuntimeContext();
        serverRuntimeContext.setInboundServerDeliver(inboundServerDeliver());
        serverRuntimeContext.setProtocolChannelRegistry(protocolChannelRegistryImpl());
        serverRuntimeContext.setMessagePipeline(messagePipeline);
        serverRuntimeContext.setOutboundServerDeliver(outboundServerDeliver());
        serverRuntimeContext.setWorker(protocolStanzaWorker());
        return serverRuntimeContext;
    }

    /***
     * redis监听器
     * @return
     */
    @Autowired
   private RedisConnectionFactory redisConnectionFactory;
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(){
        RedisMessageListenerContainer messageListenerContainer=new RedisMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(redisConnectionFactory);
        return  messageListenerContainer;
    }

    @Bean
    public ChannelTopic  channelTopic(){
        ChannelTopic channelTopic=new ChannelTopic(aiChatProperties.getDomain());
        return channelTopic;
    }

}
